const express = require("express");
const pool = require("../pool.js");

const beauRouter = express.Router();
module.exports = beauRouter;

// 美容师登录
beauRouter.post("/login", (req, res,next) => {
  var bname = req.body.bname;
  var bpwd = req.body.bpwd;
  var sql = `SELECT realname FROM beautician where bname=? and bpwd=MD5(?)`;
  pool.query(sql, [bname, bpwd], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 200, msg: "登录成功", data: r });
    } else {
      res.send({ code: 201, msg: "登录失败" });
    }
  });
});
// 美容师注册
beauRouter.post("/register", (req, res,next) => {
  var bname = req.body.bname;
  var realname = req.body.realname;
  var bpwd = req.body.bpwd;
  var bimg = req.body.bimg;
  var scope = req.body.scope;
  var btime = req.body.btime;
  var bphone = req.body.bphone;
  var bstate = req.body.bstate;
  if (bname == "" || realname == "" || bpwd == "") {
    res.send({ code: 202, msg: "输入不能为空" });
    return;
  }
  if (!/^\w{6,10}$/.test(bname)) {
    res.send({ code: 203, msg: "账号格式不正确" });
    return;
  }
  if (!/^\w{6,12}$/.test(bpwd)) {
    res.send({ code: 204, msg: "密码格式不正确" });
    return;
  }
  if (bphone != "" && !/^1[3-9]\d{9}$/.test(bphone)) {
    res.send({ code: 205, msg: "手机号格式不正确" });
    return;
  }
  var sql1 = `select bid from beautician where bname=?`;
  pool.query(sql1, [bname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 201, msg: "美容师账号已存在" });
    } else {
      var sql2 = `insert into beautician values(replace(UUID(),'-',''),?,?,MD5(?),?,?,?,?,?)`;
      pool.query(
        sql2,
        [bname, realname, bpwd, bimg, scope, btime, bphone, bstate],
        (err, r) => {
          if (err) {
            next(err);
            return;
          }
          res.send({ code: 200, msg: "美容师注册成功" });
        }
      );
    }
  });
});

// 查找所有美容师
beauRouter.get("/findAll", (req, res,next) => {
  var page = req.query.page || 1;
  const pagesize = 10;
  var offset = (page - 1) * pagesize;
  var sql = `select count(bid) as count from beautician`;
  pool.query(sql, (err, r) => {
    if (err) {
      next(err);
      return;
    }
    let pagecount = Math.ceil(r[0].count / pagesize);
    sql = `SELECT * FROM beautician limit ?,?`;
    pool.query(sql, [offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});

// 美容师修改密码
beauRouter.put("/updatePwd", (req, res,next) => {
  var bname = req.body.bname;
  var bpwd = req.body.bpwd;
  if (!/^\w{6,12}$/.test(bpwd)) {
    res.send({ code: 202, msg: "密码格式错误" });
    return;
  }
  var sql1 = `SELECT bid FROM beautician where bname=?`;
  pool.query(sql1, [bname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      var sql2 = `update beautician set bpwd=MD5(?) where bname=?`;
      pool.query(sql2, [bpwd, bname], (err, r) => {
        if (err) {
          next(err);
          return;
        }
        res.send({ code: 200, msg: "修改成功" });
      });
    } else {
      res.send({ code: 201, msg: "找不到账号" });
    }
  });
});

// 美容师修改个人信息
beauRouter.put("/update", (req, res,next) => {
  var bname = req.body.bname;
  var realname = req.body.realname;
  var bpwd = req.body.bpwd;
  var bimg = req.body.bimg;
  var scope = req.body.scope;
  var btime = req.body.btime;
  var bphone = req.body.bphone;
  var bstate = req.body.bstate;
  if (
    bname == "" ||
    realname == "" ||
    bpwd == "" ||
    scope == "" ||
    btime == "" ||
    bstate == ""
  ) {
    res.send({ code: 202, msg: "输入不能为空" });
    return;
  }
  if (!/^\w{6,12}$/.test(bpwd)) {
    res.send({ code: 204, msg: "密码格式不正确" });
    return;
  }
  if (bphone != "" && !/^1[3-9]\d{9}$/.test(bphone)) {
    res.send({ code: 205, msg: "手机号格式不正确" });
    return;
  }
  var sql1 = `select bid from beautician where bname=?`;
  pool.query(sql1, [bname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length == 0) {
      res.send({ code: 201, msg: "美容师账号不存在" });
    } else {
      var sql2 = `update beautician set realname=?, bpwd=MD5(?), bimg=?, scope=?, btime=?, bphone=?, bstate=? where bname=?`;
      pool.query(
        sql2,
        [realname, bpwd, bimg, scope, btime, bphone, bstate, bname],
        (err, r) => {
          if (err) {
            next(err);
            return;
          }
          res.send({ code: 200, msg: "修改成功" });
        }
      );
    }
  });
});

// 查找美容师姓名
beauRouter.get("/find", (req, res,next) => {
  var bname = req.query.bname;
  var sql = `SELECT realname FROM beautician where bname=?`;
  pool.query(sql, [bname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, data: r });
  });
});

// 根据美容师账号删除
beauRouter.delete("/delete", (req, res,next) => {
  var bname = req.query.bname;
  var sql = `delete from beautician where bname=?`;
  pool.query(sql, [bname], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (!r.affectedRows) {
      res.send({ code: 201, msg: "无此美容师！" });
    } else {
      res.send({ code: 200, msg: "删除成功" });
    }
  });
});
